<?php
if($_GET['action']=='del')
{
	delete('tb_insure',"id = '".$_GET[id]."'");
	echo "<script>alert('ลบข้อมูลสำเร็จ');history.back();</script>";	
}
if($_POST["hdnCmd"] == "Update")
{
	$data = array(
	"b"=>$_POST['b'],
	"f"=>$_POST['f'],
	"t"=>$_POST['t'],
	"s"=>$_POST['s'],
	"i"=>$_POST['i'],
	"h"=>$_POST['h'],
	"u"=>$_POST['u'],
	"v"=>$_POST['v'],
	"w"=>$_POST['w'],
	"l"=>$_POST['l'],
	"m"=>$_POST['m'],
	"n"=>$_POST['n'],
	"o"=>$_POST['o'],
	"p"=>$_POST['p'],
	"q"=>$_POST['q'],
	"j"=>$_POST['j']
	);
	update("tb_insure",$data,"id = '".$_POST['id']."'");
	?>
<SCRIPT LANGUAGE="JavaScript">alert("update data suscessfully");history.back();</script>	
<?
}

if($_POST["hdnCmd"] == "Add")
{
	$data = array(
	"b"=>$_POST['b2'],
	"f"=>$_POST['f2'],
	"t"=>$_POST['t2'],
	"s"=>$_POST['s2'],
	"i"=>$_POST['i2'],
	"h"=>$_POST['h2'],
	"u"=>$_POST['u2'],
	"v"=>$_POST['v2'],
	"w"=>$_POST['w2'],
	"l"=>$_POST['l2'],
	"m"=>$_POST['m2'],
	"n"=>$_POST['n2'],
	"o"=>$_POST['o2'],
	"p"=>$_POST['p2'],
	"q"=>$_POST['q2'],
	"j"=>$_POST['j2'],
	"insure_type"=>$_POST['insure_type'],
	"company_id"=>$_POST['company_id']
	);
	insert("tb_insure",$data);
	?>
<SCRIPT LANGUAGE="JavaScript">alert("add data suscessfully");window.location='?mode=insure_list.php';</script>		
<?
	//header("location:$_SERVER[PHP_SELF]");
	//exit();
}


$page = (isset($_GET['page']))? intval($_GET['page']) : 1;
$limit_end = 20;
$limit_start = ($page-1)*$limit_end;
$send="mode=insure_list.php";
$sql = "select * from tb_insure where 1";
if($_GET['type'] !="")
{
	$send .= "&type=".$_GET['type'];
	$sql .=" and (insure_type = '".$_GET['type']."')";
}
if($_GET['company'] !="")
{
	$send .= "&company=".$_GET['company'];
	$sql .=" and (company_id = '".$_GET['company']."')";
}
$sql .=" order by id desc";
//if($_GET['type']<>"")
//{
//	$send = "mode=insure_list.php&type=".$_GET['type'];
//	$sql = "SELECT * FROM tb_insure where insure_type = '".$_GET['type']."' order by id desc" ;
//}else if($_GET['company']<>"")
//{
//	$send = "mode=insure_list.php&company=".$_GET['company'];
//	$sql = "SELECT * FROM tb_insure where company_id = '".$_GET['company']."' order by id desc" ;
//}else{
//	$send = "mode=insure_list.php";
//	$sql = "SELECT * FROM tb_insure order by id desc" ;
//}
$qr = select($sql. " LIMIT " . $limit_start. "," . $limit_end); 
$total=count($qr);
$rows = mysql_num_rows(mysql_query($sql));
$i=0;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
</script>
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="15" align="left" class="headmenu"><img src="image/bar_left.gif" width="15" height="42" /></td>
    <td align="left" bgcolor="#434343" class="headmenu">ประกันทั้งหมด</td>
    <td width="15" align="left" class="headmenu"><img src="image/bar_right.gif" width="15" height="42" /></td>
  </tr>
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="margin-top:20px;">
  <tr>
    <td width="23%"><form name="form" id="form">
    เลือกประเภท :
    <select name="jumpMenu" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
      <option selected="selected" value="admin.php?mode=insure_list.php">เลือกประเภท</option>
      <?php
	  		$sql_t = "select * from tb_insure_type order by id asc";
			$qr_t = select($sql_t);
			$j=0;
			while($j<count($qr_t))
			{
				$rs_t = $qr_t[$j];
				$j++;
	  	?>
        <option value="admin.php?mode=insure_list.php&type=<?php echo $rs_t['id'] ?>&company=<?=$_GET['company']?>"<? if($_GET['type']==$rs_t['id']){ echo 'selected="selected"';} ?>><?php echo $rs_t['name'];?></option>
        <?php
			}
		?>
      </select>
    </form></td>
    <td width="77%" align="left"><form name="form" id="form">
      เลือกบริษัท :
      <select name="jumpMenu" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
      <option selected="selected" value="admin.php?mode=insure_list.php">เลือกตามบริษัท</option>
      <?php
	  		$sql_t = "select * from tb_company order by id asc";
			$qr_t = select($sql_t);
			$j=0;
			while($j<count($qr_t))
			{
				$rs_t = $qr_t[$j];
				$j++;
	  	?>
        <option value="admin.php?mode=insure_list.php&company=<?php echo $rs_t['id'] ?>&type=<?=$_GET['type']?>"<? if($_GET['company']==$rs_t['id']){ echo 'selected="selected"';} ?>><?php echo $rs_t['company_name'];?></option>
        <?php
			}
		?>
      </select>
    </form></td>
  </tr>
</table>
<form action="?mode=insure_list.php" method="post" id="frmMain" name="frmMain">
<input type="hidden" name="hdnCmd" value="">
<table width="100%" cellpadding="2" cellspacing="1" bgcolor="#000000" >
                    <tr>
                      <td rowspan="2" bgcolor="#75ACFF"><div align="center" class="list">บริษัท</div></td>
                      <td rowspan="2" bgcolor="#75ACFF"><div align="center" class="list">ซ่อม</div></td>
                      <td colspan="2" bgcolor="#8CBAFF"><div align="center" class="list">รับผิดชอบบุคคลภายนอก</div></td>
                      <td colspan="2" bgcolor="#A6CAFF"><div align="center" class="list">ตัวรถเอาประกันภัย</div></td>
                      <td colspan="4" bgcolor="#B3D1FF"><div align="center" class="list">ความคุ้มครองตามเอกสารแนบท้าย</div></td>
                      <td rowspan="2" bgcolor="#BBD7FF"><div align="center" class="list">ความรับผิดชอบส่วนแรก</div></td>
                      <td rowspan="2" bgcolor="#BBD7FF"><div align="center" class="list">รถฉุกเฉิน</div></td>
                      <td rowspan="2" bgcolor="#BBD7FF"><div align="center" class="list">รับรถ</div></td>
                      <td rowspan="2" bgcolor="#BBD7FF"><div align="center" class="list">ปีที่รับ</div></td>
                      <td rowspan="2" bgcolor="#BBD7FF"><div align="center" class="list">ถ่ายรูป</div></td>
                      <td rowspan="2" bgcolor="#BBD7FF"><div align="center" class="list">ที่นั่ง</div></td>
                      <td rowspan="2" bgcolor="#BBD7FF"><div align="center" class="list">ราคาปกติ</div></td>
                 
                      <td colspan="2" rowspan="2" bgcolor="#D9E8FF"><div align="center" class="list">ตัวเลือก<br>
                      </div></td>
                      </tr>
                    <tr>
                      <td bgcolor="#A6CAFF"><div align="center" class="list">ทรัพย์สิน</div></td>
                      <td bgcolor="#A6CAFF"><div align="center" class="list">บุคคล</div></td>
                      <td bgcolor="#B3D1FF"><div align="center" class="list">สูญหาย/<br />
                        ไฟไหม้</div></td>
                      <td bgcolor="#B3D1FF"><div align="center" class="list">การชน</div></td>
                      <td bgcolor="#BBD7FF"><div align="center" class="list">เสียชีวิต สูญเสียอวัยวะ ทุพพลภาพถาวร</div></td>
                      <td bgcolor="#BBD7FF"><div align="center" class="list">ค่ารักษา<br />
                        พยาบาล</div></td>
                      <td colspan="2" bgcolor="#BBD7FF"><div align="center" class="list">ประกันตัว<br />
                        ผู้ขับขี่</div></td>
                      </tr>
                    <?php
						while($i<$total)
						{
							if($bg=='#ffffff'){$bg = '#D9E8FF';}else{$bg = '#ffffff';}
							$rs = $qr[$i];
							$i++;
							if($_GET[action]=='edit' and $_GET[id]==$rs['id'])
							{
					?>
                            <tr bgcolor="<?=$bg;?>">
                              <td align="center"><input name="b" type="text" id="b" size="5" value="<?=$rs['b']?>" /></td>
                              <td align="center"><input name="f" type="text" id="f" size="5" value="<?=$rs['f']?>" /></td>
                              <td align="center"><input name="t" type="text" id="t" size="5" value="<?=$rs['t']?>" /></td>
                              <td align="center"><input name="s" type="text" id="s" size="5" value="<?=$rs['s']?>" /></td>
                              <td align="center"><input name="i" type="text" id="i" size="5" value="<?=$rs['i']?>" /></td>
                              <td align="center"><input name="h" type="text" id="h" size="5" value="<?=$rs['h']?>" /></td>
                              <td align="center"><input name="u" type="text" id="u" size="5" value="<?=$rs['u']?>" /></td>
                              <td align="center"><input name="v" type="text" id="v" size="5" value="<?=$rs['v']?>" /></td>
                              <td colspan="2" align="center"><input name="w" type="text" id="w" size="5" value="<?=$rs['w']?>" /></td>
                              <td align="center"><input name="l" type="text" id="l" size="5" value="<?=$rs['l']?>" /></td>
                              <td align="center"><input name="m" type="text" id="m" size="5" value="<?=$rs['m']?>" /></td>
                              <td align="center"><input name="n" type="text" id="n" size="5" value="<?=$rs['n']?>" /></td>
                              <td align="center"><input name="o" type="text" id="o" size="5" value="<?=$rs['o']?>" /></td>
                              <td align="center"><input name="p" type="text" id="p" size="5" value="<?=$rs['p']?>" /></td>
                              <td align="center"><input name="q" type="text" id="q" size="5" value="<?=$rs['q']?>" /></td>
                              <td align="center"><input name="j" type="text" id="j" size="5" value="<?=$rs['j']?>" />
                                <input name="id" type="hidden" id="id" value="<?=$rs['id']?>" />
                                </td>
                              
                              <td colspan="2">
                                  <input name="btnAdd" type="button" id="btnUpdate" value="Update" OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();">
	  							  <input name="btnAdd" type="button" id="btnCancel" value="Cancel" OnClick="window.location='?mode=insure_list.php&id=<?=$rs["id"];?>&page=<?=$page?>&type=<?=$_GET['type']?>';">
                                </a>
                              	</td>
                              </tr>
                    <?php
							}
							else
							{
					?>	
                    		<tr bgcolor="<?=$bg;?>">
                              <td bgcolor=""><div align="center" class="list"><?=$rs['b']?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=$rs['f']?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=number_format($rs['t'])?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=number_format($rs['s'])?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=number_format($rs['i'])?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=number_format($rs['h'])?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=number_format($rs['u'])?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=number_format($rs['v'])?></div></td>
                              <td colspan="2" bgcolor=""><div align="center" class="list"><?=number_format($rs['w'])?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=number_format($rs['l'])?></td>
                              <td bgcolor=""><div align="center" class="list"><?=$rs['m']?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=$rs['n']?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=$rs['o']?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=$rs['p']?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=$rs['q']?></div></td>
                              <td bgcolor=""><div align="center" class="list"><?=number_format($rs['j'],2)?><br />
                              </div></td>
                              
                              <td align="center" bgcolor="<?=$bg;?>">
                              <a href="admin.php?mode=insure_list.php&action=edit&id=<?=$rs["id"];?>&page=<?=$page?>&type=<?=$_GET['type']?>&company=<?=$_GET['company']?>"><img src="image/page_edit.png" width="16" height="16" /></a>
                              </td>
                              <td align="center" bgcolor=""><a onClick="return confirm('ต้องการจะลบข้อมูล?')" href="?mode=insure_list.php&id=<?=$rs['id']?>&action=del"><img src="image/delete.png" width="16" height="16" border="0" /></a></td>
                              </tr>
                    <?php
							}
                        }
						if($_GET['type'] and $_GET['company'])
						{
					?>
                     <tr bgcolor="<?=$bg;?>">
                              <td align="center"><input name="b2" type="text" id="b2" size="5" /></td>
                              <td align="center"><input name="f2" type="text" id="f2" size="5" /></td>
                              <td align="center"><input name="t2" type="text" id="t2" size="5" /></td>
                              <td align="center"><input name="s2" type="text" id="s2" size="5" /></td>
                              <td align="center"><input name="i2" type="text" id="i2" size="5" /></td>
                              <td align="center"><input name="h2" type="text" id="h2" size="5" /></td>
                              <td align="center"><input name="u2" type="text" id="u2" size="5" />
                              <input name="insure_type" type="hidden" id="insure_type" value="<?=$_GET['type']?>" />
                              <input name="company_id" type="hidden" id="company_id" value="<?=$_GET['company']?>" /></td>
                              <td align="center"><input name="v2" type="text" id="v2" size="5" /></td>
                              <td colspan="2" align="center"><input name="w2" type="text" id="w2" size="5" /></td>
                              <td align="center"><input name="l2" type="text" id="l2" size="5" /></td>
                              <td align="center"><input name="m2" type="text" id="m2" size="5" /></td>
                              <td align="center"><input name="n2" type="text" id="n2" size="5" /></td>
                              <td align="center"><input name="o2" type="text" id="o2" size="5" /></td>
                              <td align="center"><input name="p2" type="text" id="p2" size="5" /></td>
                              <td align="center"><input name="q2" type="text" id="q2" size="5" /></td>
                              <td align="center"><input name="j2" type="text" id="j2" size="5" /></td>
                      		  <td colspan="2"><input name="btnAdd" type="button" id="btnUpdate" value="Add" OnClick="frmMain.hdnCmd.value='Add';frmMain.submit();">
                   	   		</td>
    					</tr>
    			<?
						}
				?>
                  </table>
                  </form>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="30"><?php if(($rows/$limit_end) > 1){ 
		echo "\n<div>" . page_navi($page, $limit_end, 4, $rows, $send,"mynavic","mynavi") . " </div>";
	}?></td>
  </tr>
</table>


</body>
</html>
